package com.example.reggie_take_out.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.reggie_take_out.commen.R;
import com.example.reggie_take_out.entry.User;
import com.example.reggie_take_out.service.UserService;
import com.example.reggie_take_out.utils.SMSUtils;
import com.example.reggie_take_out.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private SMSUtils sms;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 发送验证码 给客户
     * @param
     * @param
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, user.getPhone());
        int count = userService.count(queryWrapper);

        if(count>0){
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}",code);

            User user1 = userService.getOne(queryWrapper);
            String email = user1.getEmail();

           sms.sendMessage(code,email);

           // session.setAttribute("code",code);
           //使用redis后 就不需要把验证码存到session当中 而是存在redis数据库当中
            redisTemplate.opsForValue().set("code",code,1, TimeUnit.MINUTES);
            return R.success("验证码已发出");
        }
        return R.error("验证码发送失败");
    }


    @PostMapping("/login")
    public R<String> login(@RequestBody Map map,HttpSession session){
        //获得手机号
        String phone = map.get("phone").toString();
        //获得验证码
        String code = map.get("code").toString();

        //从session中获得保存的验证码

       // String code1 = (String)session.getAttribute("code");
        //从redis中获得code
        String code1 = redisTemplate.opsForValue().get("code");

        if(code1 !=null && code1.equals(code)){
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User one = userService.getOne(queryWrapper);
            //登录成功后 要把用户id存入到session  否则会被过滤器拦截
            session.setAttribute("user",one.getId());
            //登录成功后 还要把redis中的验证码删除
            redisTemplate.delete("code");
           return R.success("登录成功");
        }
        return R.error("登陆失败");

    }
}
